/**
 * Created by hiephn on 2014/08/26.
 */
require(['jquery','knockout'], function ($, ko) {
    ko.bindingHandlers.uniform = {
        init: function(el) {
            var $el = $(el);
            $el.uniform();
        }
    };
    ko.bindingHandlers.validate = {
        init: function (el, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var $elem = $(el),
                options = valueAccessor(),
                validator = null;
            if (options) {
                if (options.cfg) {
                    validator = $elem.validate(ko.unwrap(options.config));
                } else {
                    validator = $elem.validate();
                }
                if (typeof options.submit != "function")
                    throw new Error("The value for a submit binding must be a function");
                ko.utils.registerEventHandler(el, "submit", function (event) {

                    var handlerReturnValue;
                    var value = options.submit;
                    try {
                        if ($(el).valid()) {
                            handlerReturnValue = value.call(bindingContext['$data'], el);
                        }
                    } finally {
                        if (handlerReturnValue !== true) {
                            if (event.preventDefault)
                                event.preventDefault();
                            else
                                event.returnValue = false;
                        }
                    }
                });
            } else {
                validator = $elem.validate();
            }
        }
    }
})